package com.itheima.d5_dynamic_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

public class Demo2 {
    public static void main(String[] args) {
        // 被代理对象
        BaoQiang baoQiang = new BaoQiang();

        ClassLoader classLoader = BaoQiang.class.getClassLoader();
        Class<?>[] interfaces = BaoQiang.class.getInterfaces();
        Star songjiji = (Star) Proxy.newProxyInstance(classLoader, interfaces, new BaoqiangHandler(baoQiang));
        songjiji.sleep();
        songjiji.liveShow(10000);

    }
}
class BaoqiangHandler implements InvocationHandler{
    private BaoQiang baoQiang;
    public BaoqiangHandler(BaoQiang baoQiang){
        this.baoQiang = baoQiang;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String name = method.getName();
        if (name.equals("sleep")){
            System.out.println("宋吉吉订了一家五星级酒店");
            baoQiang.sleep();
            System.out.println("宋吉吉 退房");
        }else {
            return method.invoke(baoQiang,args);
        }
        return null;
    }
}
//被代理对象
class BaoQiang implements Star {
    @Override
    public double liveShow(double money) {
        System.out.println("宝强参加真人秀,睡了"+money);
        return money;
    }

    @Override
    public void sleep() {
        System.out.println("宝强累了，需要睡觉");
    }
}

interface Star{
    double liveShow(double money);
    void sleep();
}
